#include<stdio.h>
#include<stdlib.h>

typedef struct PolyNode 
{
    int coef; // 系数
    int exp;  // 指数
    struct PolyNode* next;
}PolyNode;

void creatPoly(PolyNode* poly)
{
    PolyNode *newnode, *tail;
    tail = poly;
    int n;
    scanf("%d", &n);
    while(n--)
    {
        newnode = (PolyNode*)malloc(sizeof(PolyNode));
        scanf("%d %d", &newnode->coef, &newnode->exp);
        tail->next = newnode;
        tail = newnode;
    }
    tail->next = NULL;
}

void printPoly(PolyNode* poly)
{
    PolyNode* cur = poly->next;
    while(cur)
    {
        if(cur == poly->next)
        {
            printf("%d %d", cur->coef, cur->exp);
        }
        else
        {
            printf(" %d %d", cur->coef, cur->exp);
        }
        cur = cur->next;
    }
}

void addPoly(PolyNode *poly1, PolyNode *poly2)
{
    PolyNode *result;
    result = (PolyNode*)malloc(sizeof(PolyNode));
    result->next = NULL;
    
    PolyNode *p = poly1->next, *q = poly2->next, *tail, *newnode;
    tail = result;

    while(p && q)
    {
        newnode = (PolyNode*)malloc(sizeof(PolyNode));
        if(p->exp > q->exp)
        {
            newnode->coef = p->coef;
            newnode->exp = p->exp;
            newnode->next = NULL;
            tail->next = newnode;
            tail = newnode;
            p = p->next;
        }
        else if(p->exp < q->exp)
        {
            newnode->coef = q->coef;
            newnode->exp = q->exp;
            newnode->next = NULL;
            tail->next = newnode;
            tail = newnode;
            q = q->next;
        }
        else
        {
            if(p->coef + q->coef == 0)
            {
                p = p->next;
                q = q->next;
                free(newnode);
            }
            else
            {
                newnode->coef = p->coef + q->coef;
                newnode->exp = p->exp;
                newnode->next = NULL;
                tail->next = newnode;
                tail = newnode;
                p = p->next;
                q = q->next;
            }
        }
    }

    if(p != NULL)
    {
        tail->next = p;
    }
    if(q != NULL)
    {
        tail->next = q;
    }

    if(result->next == NULL)
    {
        free(result);
        printf("0 0");
    }
    else
    {
        printPoly(result);
    }
}

void multipoly(PolyNode* list1, PolyNode* list2)
{
    PolyNode *result, *p = list1->next, *q , *tail, *newNode;
    result = (PolyNode*)malloc(sizeof(PolyNode*));
    result->next = NULL;
    tail = result;
    while (p)
    {
        q = list2->next;
        while (q)
        {
            // 创建新建的newNode
            newNode = (PolyNode*)malloc(sizeof(PolyNode));
            newNode->coef = p->coef * q->coef;
            newNode->exp = p->exp + q->exp;
            newNode->next = NULL;

            PolyNode* temp = result;
            // 找到合适的插入位置
            while (temp->next && newNode->exp < temp->next->exp)
            {
                temp = temp->next;
            }
            // 插入或者合并同类项
            if (temp->next == NULL || newNode->exp > temp->next->exp)
            {
                newNode->next = temp->next;
                temp->next = newNode;
            }
            else if (newNode->exp == temp->next->exp)
            {
                temp->next->coef += newNode->coef;
                // 如果合并同类项后系数为0，得删除这个节点
                if(!temp->next->coef){
                    PolyNode* deleteNode=temp->next;
                    temp->next=deleteNode->next;
                    free(deleteNode);
                }
                free(newNode);
            }
            q = q->next;
        }
        p = p->next;
    }
    if (!result->next)
    {
        free(result);
        printf("0 0");
    }
    else
    {
        printPoly(result);
    }
}

int main()
{
    PolyNode *poly1, *poly2;
    poly1 = (PolyNode*)malloc(sizeof(PolyNode));
    poly2 = (PolyNode*)malloc(sizeof(PolyNode));
    poly1->next = poly2->next = NULL;

    creatPoly(poly1);
    creatPoly(poly2);

    multipoly(poly1, poly2);
    printf("\n");
    addPoly(poly1, poly2);

    //printPoly(poly1);
    //printf("\n");
    //printPoly(poly2);
}
